
package net.atremmedia.gwt.actionjava.examples.sgi;

import actionjava.events.tick.TickEvent;
import actionjava.session.view.BaseView2D;
import actionjava.session.view.CanvasInUseException;
import actionjava.session.view.CanvasNotFoundException;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JsArray;

public class SGI2RegHeader extends BaseView2D implements EntryPoint
{
	public SGI2RegHeader() throws CanvasNotFoundException, CanvasInUseException
	{
		super("ajTest");
	}

	@Override
	public void run()
	{
		HighlightVO[] highlights = parseFeed();
		createImgGallery(createGalleryFeed(highlights));
		createHighlights(highlights);
		createHeading();
	}

	private HighlightVO[] parseFeed()
	{
		JsArray<HightLightJSO> cs = getHighlights();
		HighlightVO[] highlights = new HighlightVO[cs.length()];
		for(int i = 0;i < cs.length();++i) {
			HightLightJSO highLightJSO = cs.get(i);
			highlights[i] = new HighlightVO(highLightJSO.getTitle(), highLightJSO.getColor(), highLightJSO.getImage());

			JsArray<HightLightLinkJSO> csLinks = highLightJSO.getLinks();
			HighlightLinkVO[] highlightLinks = new HighlightLinkVO[csLinks.length()];
			for(int j = 0;j < csLinks.length();++j) {
				HightLightLinkJSO highLightLinkJSO = csLinks.get(j);
				highlightLinks[j] = new HighlightLinkVO(highLightLinkJSO.getHref(), highLightLinkJSO.getName());
			}

			highlights[i].setLinks(highlightLinks);
		}

		return highlights;
	}

	public static native JsArray<HightLightJSO> getHighlights() /*-{
																return $wnd.ajTestFeed;
																}-*/;

	private void createHeading()
	{
		addChild(new Heading());
	}

	private void createImgGallery(ImgGalleryItemVO[] imgGalleryItems)
	{
		addChild(new ImgGallery(imgGalleryItems));
		// addChild(new ImgGallery(new ImgGalleryItemVO[] { new ImgGalleryItemVO(260, 110, "OUR COMMITMENT", "#f18f30", "img/gallery1.png"), new ImgGalleryItemVO(280, 110, "SINGAPORE", "#3faf4f",
		// "img/gallery2.png"), new ImgGalleryItemVO(270, 110, "LASTEST NEWS", "#1073b6", "img/gallery3.png"), new ImgGalleryItemVO(275, 110, "PARTNERSHIP", "#a17c55", "img/gallery4.png") }));
	}

	private ImgGalleryItemVO[] createGalleryFeed(HighlightVO[] highlights)
	{
		ImgGalleryItemVO[] galleryItems = new ImgGalleryItemVO[4];
		for(int i = 0;i < 4;i++) {
			galleryItems[i] = new ImgGalleryItemVO(260, 110, highlights[i].getTitle(), highlights[i].getColor(), highlights[i].getImage());
		}

		return galleryItems;
	}

	private void createHighlights(HighlightVO[] highlights)
	{
		for(int i = 0;i < highlights.length;i++) {
			HighlightVO highlight = highlights[i];
			createHighlight((25 + (250 * i)), 220, highlight);
		}

		// createHighlight(275, 220, "#3faf4f", "SINGAPORE", new HighlightVO[] { new HighlightVO("http://www.example.com", "bla1"), new HighlightVO("http://www.example.com/2", "bla2"), new
		// HighlightVO("http://www.example.com/3", "bla3") });
		// createHighlight(525, 220, "#1073b6", "LASTEST NEWS", new HighlightVO[] { new HighlightVO("http://www.example.com", "bla1"), new HighlightVO("http://www.example.com/2", "bla2"), new
		// HighlightVO("http://www.example.com/3", "bla3") });
		// createHighlight(775, 220, "#a17c55", "PARTNERSHIP", new HighlightVO[] { new HighlightVO("http://www.example.com", "bla1"), new HighlightVO("http://www.example.com/2", "bla2"), new
		// HighlightVO("http://www.example.com/3", "bla3") });
	}

	private void createHighlight(double x, double y, HighlightVO highlight)
	{
		HighlightBlock highlightBlock = new HighlightBlock(x, y, this, highlight.getColor(), highlight.getTitle(), highlight);
	}

	@Override
	public void onRenderTick(TickEvent event)
	{
	}
}
